package com;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;


import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.File;
import java.io.IOException;

/**原数据
     A:B,C,D,F,E,O
     B:A,C,E,K
     C:F,A,D,I
     D:A,E,F,L
     E:B,C,D,M,L
     F:A,B,C,D,E,O,M
     G:A,C,D,E,F
     H:A,C,D,E,O
     I:A,O
     J:B,O
     K:A,C,D
     L:D,E,F
     M:E,F,G
     O:A,H,I,J
 将原数据以  A I,K,C,B,G,F,H,O,D,的形式输出，找出所有有A好友的人，即找出哪些人的共同好友是A
 *
 * @Auther: zhezhe
 * @Date: 2018/11/210:55
 * @Description:
 */
public class CommonFriends {

    public static class MapFriends extends Mapper<LongWritable, Text,Text,Text>{

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //读取一行数据 A:B,C,D,F,E,O，该行数据代表A的好友有B,C,D,F,E,O
            String line = value.toString();
            String[] s1 = line.split(":");

            String[] s2 = s1[1].split(",");

            //输出 好友，人   如 B,A   C,A
            for (String s : s2) {
                context.write(new Text(s),new Text(s1[0]));
            }
        }
    }


    public static class ReduceFriends extends Reducer<Text,Text,Text,Text>{
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            StringBuffer sbu = new StringBuffer();
            for (Text value : values) {
                sbu.append(value.toString() + ",");
            }
            //输出形式例如  A	 I,K,C,B,G,F,H,O,D,  找出所有有A好友的人，即谁的好友是A
            context.write(key,new Text(sbu.toString()));
        }
    }


    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);

        job.setJarByClass(CommonFriends.class);
        job.setMapperClass(MapFriends.class);
        job.setReducerClass(ReduceFriends.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        job.setNumReduceTasks(1);

        File file = new File("E:\\Mapreduce案例\\CommonFriends\\数据\\output");
        if (file.exists()){
            FileUtil.fullyDelete(file);
        }

        FileInputFormat.addInputPath(job,new Path("E:\\Mapreduce案例\\CommonFriends\\数据\\input"));
        FileOutputFormat.setOutputPath(job,new Path("E:\\Mapreduce案例\\CommonFriends\\数据\\output"));

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
